package com.syh.niceweibo.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.syh.niceweibo.R;
import com.syh.niceweibo.common.AsyncImageLoader;
import com.syh.niceweibo.common.AsyncImageLoader.ImageCallback;

public class FlowView extends LinearLayout implements View.OnClickListener, View.OnLongClickListener {

    private AnimationDrawable loadingAnimation;
    private FlowTag flowTag;
    private Context context;
    public Bitmap bitmap;
    private int columnIndex;// 图片属于第几列
//    private int rowIndex;// 图片属于第几行
    private Handler viewHandler;
    
    private ImageView mImage;
    private TextView mtvContent;
    private ImageView mivHeadIcon;
    private TextView mtvTransmitCount;


//    public FlowView(Context c, AttributeSet attrs) {
//        super(c, attrs);
//        this.context = c;
//        Init(c);
//    }

    public FlowView(Context c) {
        super(c);
        this.context = c;
        Init(c);
    }
//
//    public void setWidth(int width){
//    	mImageWidth=width;
//    }
    
    private void Init(Context c) {
    	LayoutInflater.from(c).inflate(R.layout.flow_view, this);
    	mImage=(ImageView)findViewById(R.id.image);
    	mtvContent=(TextView)findViewById(R.id.tvContent);
    	mivHeadIcon=(ImageView)findViewById(R.id.ivHeadIcon);
    	mtvTransmitCount=(TextView)findViewById(R.id.tvTransmitCount);
    	
    	mImage.setOnClickListener(this);
    	mImage.setOnLongClickListener(this);
        mImage.setAdjustViewBounds(true);

    }

    @Override
    public boolean onLongClick(View v) {
        Log.d("FlowView", "LongClick");
        Toast.makeText(context, "长按：" + this.flowTag.getFlowId(), Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    public void onClick(View v) {
        Log.d("FlowView", "Click");
        Toast.makeText(context, "单击：" + this.flowTag.getFlowId(), Toast.LENGTH_SHORT).show();
    }

    /**
     * 加载图片
     */
    public void loadImage(final boolean isReload) {
        if (getFlowTag() != null) {
        	Bitmap result=AsyncImageLoader.getInstance().loadBitmap(getFlowTag().info.getWaterFallsImage(), new ImageCallback() {
				
				@Override
				public void imageLoaded(Bitmap imageBitmap, Object tag) {
					if(imageBitmap!=null){
						loaded(imageBitmap,isReload);
					}
				}
			}, getFlowTag().info.getWaterFallsImage(),getFlowTag().getItemWidth());
        	loaded(result,isReload);
        }
    }
    
    private void loaded(Bitmap arg0,boolean isReload){
    	bitmap = arg0;
    	if (bitmap != null) {// 此处在线程过多时可能为null
    		if(isReload){
    			mImage.setImageBitmap(bitmap);
    		}else{
    			int width = bitmap.getWidth();// 获取真实宽高
                int height = bitmap.getHeight();
                
                int layoutHeight = (height * flowTag.getItemWidth()) / width;// 调整高度
                LayoutParams lp = new LayoutParams(flowTag.getItemWidth(), layoutHeight);
                mImage.setLayoutParams(lp);

                mImage.setImageBitmap(bitmap);
                Handler h = getViewHandler();
                Message m = h.obtainMessage(flowTag.what, width, layoutHeight,
                        FlowView.this);
                h.sendMessage(m);
    		}
            
        }
    }

    /**
     * 回收内存
     */
    public void recycle() {
    	mImage.setImageBitmap(null);
        if ((this.bitmap == null) || (this.bitmap.isRecycled()))
            return;
        this.bitmap.recycle();
        this.bitmap = null;
    }

    public FlowTag getFlowTag() {
        return flowTag;
    }

    public void setFlowTag(FlowTag flowTag) {
        this.flowTag = flowTag;
        mtvContent.setText(flowTag.info.weiboInfo.content);
        mtvTransmitCount.setText("("+flowTag.info.weiboInfo.transmitCount+")");
    }

    public int getColumnIndex() {
        return columnIndex;
    }

    public void setColumnIndex(int columnIndex) {
        this.columnIndex = columnIndex;
    }

//    public int getRowIndex() {
//        return rowIndex;
//    }

//    public void setRowIndex(int rowIndex) {
//        this.rowIndex = rowIndex;
//    }

    public Handler getViewHandler() {
        return viewHandler;
    }

    public FlowView setViewHandler(Handler viewHandler) {
        this.viewHandler = viewHandler;
        return this;
    }

    // class ReloadImageThread extends Thread {
    //
    // @Override
    // public void run() {
    // if (flowTag != null) {
    //
    // // BufferedInputStream buf;
    // // try {
    // // buf = new BufferedInputStream(flowTag.getAssetManager()
    // // .open(flowTag.getFileName()));
    // // bitmap = BitmapFactory.decodeStream(buf);
    // //
    // // } catch (IOException e) {
    // //
    // // e.printStackTrace();
    // // }
    // //
    // // ((Activity) context).runOnUiThread(new Runnable() {
    // // public void run() {
    // // if (bitmap != null) {// 此处在线程过多时可能为null
    // // setImageBitmap(bitmap);
    // // }
    // // }
    // // });
    //
    // new ImageAsyncLoader(){}.
    // }
    //
    // }
    // }

    // class LoadImageThread extends Thread {
    // LoadImageThread() {
    // }
    //
    // public void run() {
    //
    // if (flowTag != null) {
    //
    // BufferedInputStream buf;
    // try {
    // buf = new BufferedInputStream(flowTag.getAssetManager()
    // .open(flowTag.getFileName()));
    // bitmap = BitmapFactory.decodeStream(buf);
    //
    // } catch (IOException e) {
    //
    // e.printStackTrace();
    // }
    // // if (bitmap != null) {
    //
    // // 此处不能直接更新UI，否则会发生异常：
    // // CalledFromWrongThreadException: Only the original thread that
    // // created a view hierarchy can touch its views.
    // // 也可以使用Handler或者Looper发送Message解决这个问题
    //
    // ((Activity) context).runOnUiThread(new Runnable() {
    // public void run() {
    // if (bitmap != null) {// 此处在线程过多时可能为null
    // int width = bitmap.getWidth();// 获取真实宽高
    // int height = bitmap.getHeight();
    //
    // LayoutParams lp = getLayoutParams();
    //
    // int layoutHeight = (height * flowTag.getItemWidth())
    // / width;// 调整高度
    // if (lp == null) {
    // lp = new LayoutParams(flowTag.getItemWidth(),
    // layoutHeight);
    // }
    // setLayoutParams(lp);
    //
    // setImageBitmap(bitmap);
    // Handler h = getViewHandler();
    // Message m = h.obtainMessage(flowTag.what, width,
    // layoutHeight, FlowView.this);
    // h.sendMessage(m);
    // }
    // }
    // });
    //
    // // }
    //
    // }
    //
    // }
    // }
}
